package com.wanczy.controller;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.wanczy.pojo.User;
import com.wanczy.service.UserOneService;

@Controller
@SessionAttributes(value="user")
public class UserOneController {
	@Autowired
	private UserOneService userOneService;
	
	@RequestMapping(value="user",method=RequestMethod.POST)
	public String findByPassAndName(@RequestParam("userName")String userName,@RequestParam("userPass") String userPass,Map<String, Object> map) {
		User user = userOneService.findByUserPassAndName(userName, userPass);
		
		
		
		map.put("user", user);
	/*	HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		User us = (User) request.getSession().getAttribute("user");
		
		HttpSession session = request.getSession();
	User e =	SystemSession.get();
		System.out.println(e+"----------");*/
		if(null != user){
			return "/login/index";
		}
		
		return "/login/fail";
	}
	
	
	//查询用户信息
	@RequestMapping(value="/getUsers")
	@ResponseBody
	public ModelAndView getUsers(@RequestParam("userId") Integer userId,ModelAndView view) {
		User user =  userOneService.getUsers(userId);
		view.addObject("users", user);
		view.setViewName("/userspace/userspace");
		return view;
	}
	//修改用户信息
	@RequestMapping(value="/updUsers")
	@ResponseBody
	public Integer getUpdusers(User user,ModelAndView view) {
	  int use =  userOneService.getUpdUsers(user);
//		System.out.println(user);
//		view.addObject("updusers", user);
		//view.setViewName("/userspace/userspace");
		return use;      
		//return "redirect:/getUsers";
	}
	
}
